package com.berje.vkalarm.fragments;

import java.util.Calendar;
import java.util.List;

import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.NumericWheelAdapter;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

import com.berje.vkalarm.R;
import com.berje.vkalarm.vksdk.model.Group;

public class AlarmFragment extends Fragment {
	private static final String TAG = "vkalarm-alarm-fragment";

	private WheelView hours;
	private WheelView mins;

	private Button applyButton;
	
	private List<Group> groups;

	private IOnAlarmClickListener mCallback;

	public AlarmFragment(List<Group> groups) {
		this.groups = groups;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			mCallback = (IOnAlarmClickListener) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString() + " must implement IOnDownloadClickListener");
		}

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(TAG, "onCreate");

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		Log.d(TAG, "onCreateView");
		View view = inflater.inflate(R.layout.fragment_alarm, container, false);

		hours = (WheelView) view.findViewById(R.id.hour);
		hours.setViewAdapter(new NumericWheelAdapter(getActivity(), 0, 23));

		mins = (WheelView) view.findViewById(R.id.mins);
		mins.setViewAdapter(new NumericWheelAdapter(getActivity(), 0, 59, "%02d"));
		mins.setCyclic(true);

		// set current time
		Calendar c = Calendar.getInstance();
		int curHours = c.get(Calendar.HOUR_OF_DAY);
		int curMinutes = c.get(Calendar.MINUTE);
		hours.setCurrentItem(curHours);
		mins.setCurrentItem(curMinutes);

		applyButton = (Button) view.findViewById(R.id.applyButton);
		applyButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mCallback.onApplyButtonClick(hours.getCurrentItem(), mins.getCurrentItem());
			}

		});

		return view;
	}
}
